package com.zhihaoliang.animationtrain.animator

import android.animation.ObjectAnimator
import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Canvas
import android.graphics.RadialGradient
import android.graphics.Shader
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.zhihaoliang.animationtrain.R
import com.zhihaoliang.animationtrain.annotation.Info
import com.zhihaoliang.animationtrain.util.ShapeHolder

@Info("动画值的计算", "通过计算器TypeEvaluator定义动画插值")
class CustomEvaluatorActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.animator_activity_custom_evaluator)

        val myAnimationView = MyAnimationView(this)
        findViewById<LinearLayout>(R.id.animator_containner).addView(myAnimationView)
        findViewById<View>(R.id.start_button).setOnClickListener {
            myAnimationView.startAnimation()
        }
    }

    private data class XYHolder(var x: Float, var y: Float)

    private class BallXYHolder(var ball: ShapeHolder) {
        var xY = XYHolder(ball.x, ball.y)
            set(value) {
                ball.x = value.x
                ball.y = value.y
            }
    }

    private class XYEvaluator : TypeEvaluator<XYHolder> {
        override fun evaluate(
            fraction: Float,
            startValue: XYHolder,
            endValue: XYHolder
        ): XYHolder {
            return XYHolder(
                startValue.x + fraction * (endValue.x - startValue.x),
                startValue.y + fraction * (endValue.y - startValue.y)
            )
        }

    }

    private class MyAnimationView(context: Context) :
        View(context),
        AnimatorUpdateListener {
        var bounceAnim: ValueAnimator? = null
        var ball: ShapeHolder = createBall()
        var ballHolder: BallXYHolder = BallXYHolder(ball)
        private fun createAnimation() {
            if (bounceAnim == null) {
                val endXY = XYHolder(300f, 500f)
                bounceAnim = ObjectAnimator.ofObject(
                    ballHolder, "xY",
                    XYEvaluator(), endXY
                )
                bounceAnim!!.duration = 1500
                bounceAnim!!.addUpdateListener(this)
            }
        }

        fun startAnimation() {
            createAnimation()
            bounceAnim!!.start()
        }

        private fun createBall(): ShapeHolder {
            val circle = OvalShape()
            circle.resize(50f, 50f)
            val drawable = ShapeDrawable(circle)
            val shapeHolder = ShapeHolder(drawable)
            shapeHolder.x = 0F
            shapeHolder.y = 0F
            val red = (Math.random() * 255).toInt()
            val green = (Math.random() * 255).toInt()
            val blue = (Math.random() * 255).toInt()
            val color = -0x1000000 or (red shl 16) or (green shl 8) or blue
            val paint =
                drawable.paint //new Paint(Paint.ANTI_ALIAS_FLAG);
            val darkColor = -0x1000000 or (red / 4 shl 16) or (green / 4 shl 8) or blue / 4
            val gradient = RadialGradient(
                37.5f, 12.5f,
                50f, color, darkColor, Shader.TileMode.CLAMP
            )
            paint.shader = gradient
            shapeHolder.paint = paint
            return shapeHolder
        }

        override fun onDraw(canvas: Canvas) {
            canvas.save()
            canvas.translate(ball.x, ball.y)
            ball.shapeDrawable.draw(canvas)
            canvas.restore()
        }

        override fun onAnimationUpdate(animation: ValueAnimator) {
            invalidate()
        }

    }
}
